//
//  ZJGestureViewController.m
//  GestureDemo
//
//  Created by ZJ on 2018/8/7.
//  Copyright © 2018年 iCatch Technology Inc. All rights reserved.
//

#import "ZJGestureViewController.h"

@interface ZJGestureViewController () <UIGestureRecognizerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ZJGestureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 允许用户交互
    self.imageView.userInteractionEnabled = YES;
    
    [self tapGestureTest];
    [self longPressGestureTest];
    [self swipeGestureTest];
    [self rotationGestureTest];
    [self pinchGestureTest];
    [self panGestureTest];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// 解决手势冲突
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

#pragma mark - UIPanGestureRecognizer(拖拽)
- (void)panGestureTest {
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    
    [self.imageView addGestureRecognizer:pan];
}

- (void)pan:(UIPanGestureRecognizer *)sender {
    CGPoint p = [sender translationInView:sender.view];
    
    self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, p.x, p.y);
    
    // 恢复到初始状态
    [sender setTranslation:CGPointZero inView:sender.view];
}

#pragma mark - UIPinchGestureRecognizer(捏合，用于缩放)
- (void)pinchGestureTest {
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    
    [self.imageView addGestureRecognizer:pinch];
}

- (void)pinch:(UIPinchGestureRecognizer *)sender {
    NSLog(@"%f", sender.scale);
    
    self.imageView.transform = CGAffineTransformScale(self.imageView.transform, sender.scale, sender.scale);
    
    // 恢复初始状态
    sender.scale = 1;
}

#pragma mark - UIRotationGestureRecognizer(旋转)
- (void)rotationGestureTest {
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    rotation.delegate = self;
    
    [self.imageView addGestureRecognizer:rotation];
}

- (void)rotation:(UIRotationGestureRecognizer *)sender {
    NSLog(@"%f", sender.rotation);
    
//    self.imageView.transform = CGAffineTransformMakeRotation(sender.rotation);
    self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, sender.rotation);
    
    sender.rotation = 0; // "归零" 恢复到最初的状态
}

#pragma mark - UISwipeGestureRecognizer(轻扫)
- (void)swipeGestureTest {
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    
    UISwipeGestureRecognizer *swipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    // 扫动方向
    swipe1.direction = UISwipeGestureRecognizerDirectionLeft;
    
    [self.imageView addGestureRecognizer:swipe];
    [self.imageView addGestureRecognizer:swipe1];
}

- (void)swipe:(UISwipeGestureRecognizer *)sender {
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"从右向左");
    } else if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"从左向右");
    }
}

#pragma mark - UILongPressGestureRecognizer(长按)
- (void)longPressGestureTest {
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    
    longPress.minimumPressDuration = 3; // 长按多长时间有反应
    longPress.allowableMovement = 100; // 误差范围
    
    [self.imageView addGestureRecognizer:longPress];
}

- (void)longPress:(UILongPressGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateBegan) {
        NSLog(@"longPress");
    }
}

#pragma mark - UITapGestureRecognizer(敲击)
- (void)tapGestureTest {
    // 1.创建手势对象
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    
    tap.numberOfTapsRequired = 2; // 点几次
    tap.numberOfTouchesRequired = 2; // 几根手指
    
    // 2.对某一个 view 添加手势
    [self.imageView addGestureRecognizer:tap];
}

// 3.实现手势方法
- (void)tap:(UITapGestureRecognizer *)sender {
    NSLog(@"你点到我了!!!");
}

@end
